//
//  ItemEditViewController.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-6-6.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ItemEditViewController.h"


@implementation ItemEditViewController

@synthesize editItem;

@synthesize uvc;

@synthesize doneButton;

@synthesize titleCell;

@synthesize descriptCell;

@synthesize tagsCell;

@synthesize publicCell;

@synthesize friendCell;

@synthesize familyCell;

@synthesize txtTitle;
@synthesize txtDescription;
@synthesize txtTags;

@synthesize sPublic;
@synthesize sFriend;
@synthesize sFamily;


#pragma mark ButtonAction
-(IBAction)saveEditItem:(id)sender{
	if(self.txtTitle.text){
		// 此处增加针对标题为空白的时候，的处理，因为flickr要求只有 title 和 photo是必须的，因此作一下处理 by xhzhang[2009/07/01]
		if([self.txtTitle.text length] == 0){
			
		}else{
			self.editItem.title = self.txtTitle.text;
		}
	}
	
	if(self.txtDescription.text){
		self.editItem.description = self.txtDescription.text;
	}
	
	if(self.txtTags.text){
		self.editItem.tags = self.txtTags.text;
	}
	
	self.editItem.isPublic = self.sPublic.on;
	
	self.editItem.isFriend = self.sFriend.on;
	
	self.editItem.isFamily = self.sFamily.on;
	
	self.editItem.uploadState = US_NEEDUPLOAD;
	
	// 刷新数据
	if(self.uvc){
		[self.uvc reloadImages];
	}
	
	// 显示数据
	[self.navigationController popViewControllerAnimated:YES];
	
}

/**
 * switch点击，根据不同的点击进行权限的控制
 * UISwitch 当点击之后，先改变状态，然后提交事件到这个方法
 */
-(IBAction)clickSwitch:(id)sender{
	if(sender == sPublic){
		if(sPublic.on){
			// TODO 如果公开被打开，那么，朋友，家人默认都是打开的，不需要设置
			sFamily.enabled = NO;
			sFriend.enabled = NO;
		}else {
			sFamily.enabled = YES;
			sFriend.enabled = YES;
		}
	}
}

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.navigationItem.title = [NSString stringWithString:(NSLocalizedStringFromTable(@"EDIT_IMAGE",@"main",@""))];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.doneButton;
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
		// 针对 不同的内容进行设置
		
		NSInteger row = indexPath.row;
		NSInteger section = indexPath.section;
		switch(section){
			case 0:
				cell = [self getDescriptionCell:row];
				break;
			case 1:
				cell = [self getAccessCell:row];
				break;
		}


		// TODO iPhone 3.0 与 2.2 的区别
//#ifndef __IPHONE_3_0
//		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//#else
//        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//#endif

    }
    
    // Set up the cell...
	
	//if(editItem != nil){
//		cell.text = [editItem getCellTitle:indexPath.row];
//	}else{
//		cell.text = @"TODO Modify Code";
//	}
	
    return cell;
}

/**
 * 获取指定section的标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	NSString *ret = nil;
	switch (section) {
		case 0:
			ret = [NSString stringWithString:(NSLocalizedStringFromTable(@"BASE_INFO",@"main",@""))];
			break;
		case 1:
			ret = [NSString stringWithString:(NSLocalizedStringFromTable(@"VISIBILITY",@"main",@""))];
			break;
			
		default:
			break;
	}
	return ret;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark -
#pragma mark get_cell

-(UITableViewCell *)getDescriptionCell:(NSInteger)row{
	UITableViewCell *ret = nil;
	switch (row) {
		case 0:
			self.txtTitle.text = self.editItem.title;
			ret = titleCell;
			break;
		case 1:
			self.txtDescription.text = self.editItem.description;
			ret = descriptCell;
			break;
		case 2:
			self.txtTags.text = self.editItem.tags;
			ret = tagsCell;
			break;
	}
	return ret;
}

-(UITableViewCell *)getAccessCell:(NSInteger)row{
	UITableViewCell *ret = nil;
	switch (row) {
		case 0:
			self.sPublic.on = self.editItem.isPublic;
			ret = publicCell;
			break;
		case 1:
			self.sFriend.on = self.editItem.isFriend;
			ret = friendCell;
			break;
		case 2:
			self.sFamily.on = self.editItem.isFamily;
			ret = familyCell;
			break;
	}
	return ret;
}

#pragma mark -
#pragma mark UITextFieldDelegate Impl
/**
 * 点击Return key时候作的处理
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
	// 使用 resignFirstResponder来关闭软键盘显示
	BOOL bret = [textField resignFirstResponder];
	return bret;
}

- (void)dealloc {
    [super dealloc];
}


@end

